package com.sinosoft.dsp.platform.viewbean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import com.sinosoft.builder.templates.app.ApplicationType;
import com.sinosoft.builder.templates.app.MenuItem;
import com.sinosoft.builder.templates.app.NavigationType;
import com.sinosoft.dsp.platform.manager.ApplicationController;
import com.sinosoft.dsp.platform.services.PlatformNavigationService;
import com.sinosoft.dsp.platform.utils.SinoManagedBean;
import com.sinosoft.dsp.system.utils.Const;

@ManagedBean(name = "navigationViewBeanService")
@RequestScoped
public class NavigationViewBeanService {

	@ManagedProperty(value = "#{platformNavigationService}")
	private PlatformNavigationService navi;

	/**
	 * 验证菜单集合，去除没有权限的菜单
	 * 
	 * 
	 * @param items
	 * @return
	 */
	public List<MenuItem> checkMenuItems(List<MenuItem> items) {
		List<MenuItem> rtnItems = new ArrayList<MenuItem>();
		if (items != null) {
			for (MenuItem i : items) {
				// TODO 验证权限
				rtnItems.add(i);
			}
		}
		return rtnItems;
	}

	/**
	 * 获得权限验证后的主菜单项
	 * 
	 * @return
	 */
	public List<MenuItem> getCheckedMainMenuItems() {
		return checkMenuItems(getMainMenuRoot().getMenuItem());
	}

	/**
	 * 获得权限验证后的快速菜单项
	 * 
	 * @return
	 */
	public List<MenuItem> getCheckedFastMenuItems() {
		return checkMenuItems(getFastMenuRoot().getMenuItem());
	}

	/**
	 * 获得菜单的根节点
	 * 
	 * @param menuId
	 * @return
	 */
	public NavigationType getMenuRoot(String menuId) {
		ApplicationController ac = SinoManagedBean
				.findBean("applicationController");

		ApplicationType at = navi.getApplicationById(ac.getApplicationId());

		NavigationType menuItem = null;

		for (NavigationType nt : at.getNavigation()) {
			if (menuId.equals(nt.getId())) {
				menuItem = nt;
				break;
			}
		}

		return menuItem;
	}

	/**
	 * 获得主菜单的根节点
	 * 
	 * @return
	 */
	public NavigationType getMainMenuRoot() {
		return getMenuRoot(Const.MAIN_MENU_ID);
	}

	/**
	 * 获得主菜单的根节点元素个数
	 * 
	 * @return
	 */
	public int getMainMenuRootSize() {
		return getMainMenuRoot().getMenuItem().size();
	}

	/**
	 * 获得快速菜单的根节点
	 * 
	 * @return
	 */
	public NavigationType getFastMenuRoot() {
		return getMenuRoot(Const.FAST_MENU_ID);
	}

	/**
	 * 获得快速菜单的根节点元素个数
	 * 
	 * @return
	 */
	public int getFastMenuRootSize() {
		return getFastMenuRoot().getMenuItem().size();
	}

	public PlatformNavigationService getNavi() {
		return navi;
	}

	public void setNavi(PlatformNavigationService navi) {
		this.navi = navi;
	}

}
